<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php';
/**
 * 从决策系统取样衣反馈数据
 */
class SyncFbController extends Controller_CronJob {
	// 分批
	private $limit = 1000;
	public function run() {
		$this->save();
	}
	/**
	 * 更新
	 */
	private function save() {
		$model = new Cron_Model_Decision_Sample();
		$count = $model->getAccout();
		$page = ceil($count/$this->limit);
		for ($i=0;$i < $page;$i++) {
			$data = $model->getData($i*$this->limit, $this->limit);
			foreach ($data as $fb) {
				if (!$id = $model->existFb($fb['sku'])){
					continue;
				}
				$update = [
					'order_num'	=> $fb['order_used_amount'],
					'lose_stock' => $fb['lose_stock'],
					'tran_inventory' => $fb['stock_process'],
					'seven_sale' => $fb['seven_sale'],
					'rise_rate' => $fb['rise_rate'],
					'real_return_sample' => $fb['real_return_sample'],
					'real_pro' => $fb['real_pro'],
					'seven_sale_trend' => trim($fb['seven_sale_trend']) ? $fb['seven_sale_trend'] : implode(',', [$fb['sales_last_1'],$fb['sales_last_2'],$fb['sales_last_3'],$fb['sales_last_4'],$fb['sales_last_5'],$fb['sales_last_6'],$fb['sales_last_7']])
				];
				if ($fb['upshelf_time']) {
					$update['upshelf_time'] = $fb['upshelf_time'];
				}
				if ($fb['sample_time']) {
					$update['sample_time'] = $fb['sample_time'];
				}
				try {
					$model->updateFb($update, $id);
				} catch (Exception $e) {
					$this->log->record("SKU: ".$fb['sku'].$e->getMessage(), Log::ERR);
				}
			}
		}
		
		echo "success";
	}
}

$fb = new SyncFbController();
$fb->run();